package com.example.audiostreaming.adapters;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.audiostreaming.R;
import com.example.audiostreaming.utils.UtilLog;
import com.squareup.picasso.Picasso;
import com.ximalaya.ting.android.opensdk.model.album.Album;

import java.util.ArrayList;
import java.util.List;

/**
 * Recommend推荐中RecyclerView的适配器
 */
public class AdapterAlbumRv extends RecyclerView.Adapter<AdapterAlbumRv.InnerHolder> {
    private static final String TAG = "AdapterAlbumRv";

    private List<Album> mRecommendAlbumListData = new ArrayList<>();//推荐专辑列表数据
    private OnAlbumItemClickListener mOnAlbumItemClickListener;//推荐专辑条目点击监听器
    private OnAlbumItemLongClickListener mOnAlbumItemLongClickListener;

    /**
     * 创建每一个ItemView条目视图
     *
     * @param parent   父类RecyclerView视图，itemView条目视图测量时，要参考父类RecyclerView视图
     * @param viewType 父类RecyclerView视图是否绑定
     * @return 内部持有者
     */
    @NonNull
    @Override
    public InnerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //载入ItemView条目视图
        View itemRecommendView = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_recommend_item_rv, parent, false);
        return new InnerHolder(itemRecommendView);//返回创建新的带有ItemView条目视图的InnerHolder内部持有者
    }

    /**
     * ItemView条目视图绑定albumListData 推荐专辑列表数据
     *
     * @param holder 持有者
     * @param position 当前位置
     */
    @Override
    public void onBindViewHolder(@NonNull InnerHolder holder, final int position) {
        holder.itemView.setTag(position);//holder内部持有者中的itemView条目视图设置标签为position当前位置
        holder.setUIData(mRecommendAlbumListData.get(position));//根据position当前位置获得对应单个推荐专辑数据，并通过holder内部持有者的setData设置数据方法传入
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UtilLog.d(TAG,"条目被点击 ==> "+v.getTag());
                if (mOnAlbumItemClickListener != null) {
                    int clickPosition= (int) v.getTag();
                    mOnAlbumItemClickListener.onAlbumItemClick(clickPosition,mRecommendAlbumListData.get(clickPosition));
                }
            }
        });
        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (mOnAlbumItemLongClickListener != null) {
                    int clickPosition= (int) v.getTag();
                    mOnAlbumItemLongClickListener.onAlbumItemLongClick(clickPosition,mRecommendAlbumListData.get(clickPosition));
                }
                return true;
            }
        });
    }

    /**
     * @return 返回获取RecyclerView视图要显示ItemView条目视图的个数
     */
    @Override
    public int getItemCount() {
        if (mRecommendAlbumListData != null) {
            return mRecommendAlbumListData.size();
        }
        return 0;
    }

    /**
     * 设置RecyclerView视图要显示的数据
     *
     * @param albumListData 推荐专辑列表数据
     */
    public void setData(List<Album> albumListData) {
        if (mRecommendAlbumListData != null) {
            mRecommendAlbumListData.clear();//清掉上次数据
            mRecommendAlbumListData.addAll(albumListData);//将传过来的albumListData推荐专辑列表数据全部添加到本类中的mAlbumListData推荐专辑列表数据
        }
        //通知数据已更改,更新一下UI
        notifyDataSetChanged();
    }


    public interface OnAlbumItemClickListener {
        void onAlbumItemClick(int position, Album album);
    }

    public void setOnAlbumItemClickListener(OnAlbumItemClickListener onAlbumItemClickListener){
        this.mOnAlbumItemClickListener = onAlbumItemClickListener;
    }

    public interface OnAlbumItemLongClickListener{
        void onAlbumItemLongClick(int position, Album album);
    }
    public void setOnAlbumItemLongClickListener(OnAlbumItemLongClickListener onAlbumItemLongClickListener){
        this.mOnAlbumItemLongClickListener = onAlbumItemLongClickListener;
    }









    /**
     * 内部持有者的内部类
     */
    public class InnerHolder extends RecyclerView.ViewHolder {
        public InnerHolder(@NonNull View itemView) {
            super(itemView);
        }

        //找到控件，设置数据
        public void setUIData(Album album) {
            //专辑封面
            ImageView recommendAlbumItemCoverIv = itemView.findViewById(R.id.number);
            //专辑标题
            TextView recommendAlbumItemTitleTv = itemView.findViewById(R.id.title);
            //专辑播放数量
            TextView recommendAlbumItemPlayCountTv = itemView.findViewById(R.id.play_count);
            //专辑曲目数量
            TextView recommendAlbumItemTrackCountTv = itemView.findViewById(R.id.content_time);



            if (recommendAlbumItemCoverIv != null) {
                String coverUrlLarge = album.getCoverUrlLarge();
                if (!TextUtils.isEmpty(coverUrlLarge)) {
                    Picasso.with(itemView.getContext()).load(coverUrlLarge).into(recommendAlbumItemCoverIv);
                }else {
                    recommendAlbumItemCoverIv.setImageResource(R.mipmap.logo_icon);
                }
            }
            if (recommendAlbumItemTitleTv != null) {
                recommendAlbumItemTitleTv.setText(album.getAlbumTitle());
            }
            if (recommendAlbumItemPlayCountTv != null) {
                if (album.getPlayCount()<10000) {
                    recommendAlbumItemPlayCountTv.setText(album.getPlayCount()+"");
                }else if (album.getPlayCount()<100000000) {
                    recommendAlbumItemPlayCountTv.setText(album.getPlayCount()/10000+"万");
                }else {
                    recommendAlbumItemPlayCountTv.setText(album.getPlayCount()/100000000+"亿");
                }
            }
            if (recommendAlbumItemTrackCountTv != null) {
                recommendAlbumItemTrackCountTv.setText(album.getIncludeTrackCount()+"集");
            }
        }
    }
}
